package inglar.autotc.client.ui;

import inglar.autotc.client.Utilities;
import inglar.autotc.client.resource.ResourceHandler;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;

public class DialogBoxLogin extends DialogBox {
	private TextBox txtbxUsername;
	private PasswordTextBox pswrdtxtbxPassword;
	private VerticalPanel buttonPanel;
	private Button btnLogIn;
	private Image loading;
	private DialogBoxLogin self;

	public DialogBoxLogin() {
		setAnimationEnabled(true);
		setGlassEnabled(true);
		setHTML("Log In");
		
		VerticalPanel contentPanel = new VerticalPanel();
		contentPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		setWidget(contentPanel);
		contentPanel.setSize("100%", "100%");
		
		loading = new Image(ResourceHandler.INSTANCE.fbLoadBlue());
		
		FlexTable flexTable = new FlexTable();
		flexTable.setStyleName("loginDialogBoxFieldTable");
		contentPanel.add(flexTable);
		
		buttonPanel = new VerticalPanel();
		contentPanel.add(buttonPanel);
		buttonPanel.setHeight("35px");
		buttonPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		Label lblUsername = new Label("Username:");
		flexTable.setWidget(0, 0, lblUsername);
		
		txtbxUsername = new TextBox();
		txtbxUsername.setText("");
		flexTable.setWidget(0, 1, txtbxUsername);
		txtbxUsername.setWidth("160px");
		txtbxUsername.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getUnicodeCharCode() == KeyCodes.KEY_ENTER) callLogin();
			}
		});
		
		Label lblPassword = new Label("Password:");
		flexTable.setWidget(1, 0, lblPassword);
		
		pswrdtxtbxPassword = new PasswordTextBox();
		pswrdtxtbxPassword.setText("");
		flexTable.setWidget(1, 1, pswrdtxtbxPassword);
		pswrdtxtbxPassword.setWidth("160px");
		pswrdtxtbxPassword.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getUnicodeCharCode() == KeyCodes.KEY_ENTER) callLogin();
			}
		});
		
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		
		btnLogIn = new Button("Log in");
		btnLogIn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				callLogin();
			}
		});
		
		buttonPanel.add(btnLogIn);
		
		self = this;
	}
	
	private void callLogin() {
		setLoading();
		Utilities.login(txtbxUsername.getText(), pswrdtxtbxPassword.getText(), self);
	}
	
	public void setLoading() {
		buttonPanel.clear();
		buttonPanel.add(loading);
		txtbxUsername.setEnabled(false);
		pswrdtxtbxPassword.setEnabled(false);
	}
	
	public void reset() {
		buttonPanel.clear();
		buttonPanel.add(btnLogIn);
		txtbxUsername.setEnabled(true);
		pswrdtxtbxPassword.setEnabled(true);
	}

}
